package com.suzhe.ymall.order.controller;


import com.suzhe.ymall.common.annotation.LoginAuth;
import com.suzhe.ymall.common.pojo.ApiResEntity;
import com.suzhe.ymall.common.pojo.AppLoginUser;
import com.suzhe.ymall.common.utils.LoginUserUtils;
import com.suzhe.ymall.order.pojo.ShippingAddress;
import com.suzhe.ymall.order.service.IShippingAddressService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 发货地址 前端控制器
 * </p>
 *
 * @author suzhe
 * @since 2019-08-08
 */
@RestController
@RequestMapping("/order/shippingAddress")
@Slf4j
public class ShippingAddressController {

    @Autowired
    IShippingAddressService iShippingAddressService;

    @PostMapping("/save")
    @LoginAuth
    @ApiOperation(value="提交订单")
    public ApiResEntity save(@RequestBody ShippingAddress shippingAddress){
        log.debug("save shippingAddress:{}",shippingAddress);
        AppLoginUser appLoginUser = LoginUserUtils.getAppLoginUser();
        shippingAddress.setUserId(appLoginUser.getId());
        iShippingAddressService.saveOrUpdate(shippingAddress);
        return ApiResEntity.ok();
    }
    @GetMapping("/listAddress")
    @LoginAuth
    @ApiOperation(value="地址列表")
    public ApiResEntity listAddress(){
        AppLoginUser appLoginUser = LoginUserUtils.getAppLoginUser();
        log.debug("listAddress");
        List<ShippingAddress> addressList = iShippingAddressService.getByUserId(appLoginUser.getId());
        return ApiResEntity.ok(addressList);
    }

}
